<?php

// 项目评估
// POST project.data.cp

namespace app\api\resource\project\data;
use think\Db;
use think\Session;
use app\common\lib\Cp as CpLogic;

class Cp
{
    // 方法
    public $_method = ['POST'];
    // 前置
    public $_pre    = ['login','isCyz'];
    // 描述
    public $_description = '项目评估';
    // 参数
    public $_param  = [
        'project_id' => '项目主键',
    ];
    // 返回
    public $_return = [
        201 => '评估成功,返回评估报告',
        400 => '项目不存在|未缴纳项目评估费',
        422 => '项目主键必须',
        500 => '服务器错误',
    ];

    public function run(&$request)
    {
        $projectId = $request->post('project_id/d',0);
        
        if (0 == $projectId) return [422,'项目主键必须'];

        $memberId  = Session::get('member.id');
        
        $project = Db::name('project')->where('id',$projectId)->where('member_id',$memberId)->find();
        if ( is_null($project) ) return [400,'项目不存在'];

        if ( 0 == $project['is_cp_pay']) return [400,'未缴纳项目评估费'];

        $cpUri = CpLogic::run($projectId);

        return $cpUri ? [ 201,['cp_uri'=>$cpUri] ] : 500;
    }
}